/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.chazaqdev.etei;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.NativeSwing;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 *
 * @author mike
 */
public class Main {
    private static JettyServerHandler jettyServerHandler = new JettyServerHandler();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }

        final Splash splash = new Splash();
        centerTheGUIFrame(splash);
        splash.setVisible(true);
        NativeInterface.open();

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final MainFrame mf = new MainFrame();
                centerTheGUIFrame(splash);
                centerTheGUIFrame(mf);
                splash.setVisible(true);
                try {
                    String s = AppDataDir.getApplicationName();
                    if (!com.sun.jna.Platform.isWindows())  {
                        s = "." + s;
                    }
                    AppDataDir.createAppDataDir(s);
                    jettyServerHandler.start("");
                    WebAppContext wac = jettyServerHandler.getWebAppContext();
                      while (!wac.isStarted()) {

                        //Check every second to see if the server is running
                        Thread.currentThread().sleep(4000);
                    }
                    if (jettyServerHandler.isFailed() || wac.isFailed()) {
                        System.out.println("Jetty server startup failed.");
                        System.exit(1);
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                mf.setVisible(true);
                splash.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                splash.dispose();
            }
        });
        NativeInterface.runEventPump();

    }

    public static void centerTheGUIFrame(JFrame frame) {
        int widthWindow = frame.getWidth();
        int heightWindow = frame.getHeight();

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (widthWindow / 2); // Center horizontally.
        int Y = (screen.height / 2) - (heightWindow / 2); // Center vertically.

        frame.setBounds(X, Y, widthWindow, heightWindow);
    }

    public static void centerTheGUIFrame(AboutForm af) {
        int widthWindow = af.getWidth();
        int heightWindow = af.getHeight();

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int X = (screen.width / 2) - (widthWindow / 2); // Center horizontally.
        int Y = (screen.height / 2) - (heightWindow / 2); // Center vertically.

        af.setBounds(X, Y, widthWindow, heightWindow);
    }

    public static JettyServerHandler getJettyServerHandler() {
        return jettyServerHandler;
    }

}
